package com.lottery.system.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author: xiaoxie
 * create: 2024-09-14 10:20
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.common.utils
 * description: 邮箱工具类
 */
@Component
@Slf4j
public class MailUtil {
    @Value(value = "${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;
    /**
     * @description: 发送邮件
     * @author: xiaoxie
     * @date: 2024/9/14 10:24
     * @param: [to, subject, context]
     * @return: java.lang.Boolean
     **/
    public Boolean sendSampleMail(String to, String subject, String context) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(context);
        try{
            mailSender.send(message);
        }catch (Exception e){
            log.error("向{}发送邮件失败! ",to,e);
            return false;
        }
        return true;
        }
    }
